﻿using AutoMapper;
using System.Text.Json;
using System.Web;
using TrackSystem.Contracts;
using TrackSystem.Entities.Dtos.Var;
using TrackSystem.Entities.Extensions;
using TrackSystem.IO.Utils;

namespace TrackSystem.UI.Server.HttpRepository.Var
{

    public class VarHttpRepository : IVarHttpRepository
    {
        private readonly HttpClient client;
        private readonly IMapper mapper;

        public VarHttpRepository(HttpClient client,
            IMapper mapper)
        {
            this.client = client;
            this.mapper = mapper;
        }

        public async Task<IEnumerable<VarDto>> GetAll()
        {
            var response = await client.GetAsync("api/vardef/all");
            var content = await response.Content.ReadAsStringAsync();
            var Items = JsonSerializer.Deserialize<IEnumerable<VarDto>>(
                content,
                new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
            return Items ??= [];
        }

        public async Task<IEnumerable<VarDto>> GetAllOfSensor(int sensorId)
        {
            var response = await client.GetAsync($"api/vardef/sensor/{sensorId}");
            var content = await response.Content.ReadAsStringAsync();
            var Items = JsonSerializer.Deserialize<IEnumerable<VarDto>>(
                content,
                new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
            return Items ??= [];
        }

        public async Task<ApiResult<VarDto>> AddVar(VarDto varDto)
        {
            if (varDto == null)
                return ApiResult<VarDto>.Fail("变量数据库为空");

            var response = await client.PutAsJsonAsync("api/vardef", varDto);
            var content = await response.Content.ReadAsStringAsync();
            var result = JsonSerializer.Deserialize<ApiResult<VarDto>>(
                content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

            return result ?? ApiResult<VarDto>.Fail($"变量数据库为空 {content}");
        }


        public async Task<ApiResult<VarDto>> UpdateVar(VarDto varDto)
        {
            if (varDto == null)
                return ApiResult<VarDto>.Fail("变量数据库为空");

            var response = await client.PostAsJsonAsync("api/vardef", varDto);
            var content = await response.Content.ReadAsStringAsync();
            var result = JsonSerializer.Deserialize<ApiResult<VarDto>>(
                content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

            return result ?? ApiResult<VarDto>.Fail("变量数据库为空");
        }

        public async Task<ApiResult<VarDto>> DeleteVar(int id)
        {

            var response = await client.DeleteAsync($"api/vardef/{id}");
            var content = await response.Content.ReadAsStringAsync();
            var result = JsonSerializer.Deserialize<ApiResult<VarDto>>(
                content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

            return result ?? ApiResult<VarDto>.Fail("变量数据库为空");
        }

        public async Task<FormulaResult> TestForula(string expression)
        {
            string encodeUrl = HttpUtility.UrlEncode(expression);
            var response = await client.GetAsync($"api/vardef/test?formula={encodeUrl}");
            var content = await response.Content.ReadAsStringAsync();
            var result = JsonSerializer.Deserialize<FormulaResult>(
                content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

            return result ??= new FormulaResult();
        }
    }
}
